﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Communication;
using System.ComponentModel;

namespace Clipper.EngineerWindow
{
    /// <summary>
    /// UserArmAdjustment.xaml 的交互逻辑
    /// </summary>
    public partial class UserArmAdjustment : UserControl
    {
        public UserArmAdjustment()
        {
            InitializeComponent();
        }
        #region 属性
        short canID;
        /// <summary>
        /// 正在调试的机械臂的CanID
        /// 设置后显示数据
        /// </summary>
        public short CanID
        {
            get
            {
                return canID;
            }
            set
            {
                canID = value;
                ShowData();
            }
        }
        public string GroupText
        {
            get
            {
                return grpArm.Header.ToString();
            }
            set
            {
                grpArm.Header = value;
            }
        }
        public string TextDC
        {
            get
            {
                return txtDC.Text;
            }
            set
            {
                txtDC.Text = value;
            }
        }
        public string TextDE
        {
            get
            {
                return txtDE.Text;
            }
            set
            {
                txtDE.Text = value;
            }
        }
        public string TextDI
        {
            get
            {
                return txtDI.Text;
            }
            set
            {
                txtDI.Text = value;
            }
        }
        #endregion
        const string strEnd = "\r";//结束符
        const string DC = "2DC";//限位，修改步数
        const string DE = "3DE";//定位，修改加速度
        const string DI = "4DI";//微调吸液量，修改步数
        const string GE = "1GE";//加液位置下降量
        const string GF = "1GF";//清洗位置下降
        //string SA = "SA";//保存参数
        List<Model.Adjustment> mAdjustList = null;


        private void btnArmAdjust_Click(object sender, RoutedEventArgs e)
        {
            string btnName = ((Button)sender).Name;
            switch (btnName)
            {
                case "btnDC":
                    Can.SendCan(CanID,  DC + txtDC.Text + strEnd);
                    break;
                case "btnDE":
                    Can.SendCan(CanID,  DE + txtDE.Text + strEnd);
                    break;
                case "btnDI":
                    Can.SendCan(CanID,  DI + txtDI.Text + strEnd);
                    break;
                case "btnGE":
                    Can.SendCan(CanID, GE + txtGE.Text + strEnd);
                    break;
                case "btnGF":
                    Can.SendCan(CanID, GF + txtGF.Text + strEnd);
                    break;
                //case "btnSave"://保存
                //    Can.SendCan(CanID, motorDC + SA + strEnd);
                //    break;
            }
        }
        private void ShowData()
        {
            BLL.Adjustment adjustmentBll = new BLL.Adjustment();
            BackgroundWorker backWorker = new BackgroundWorker();
            backWorker.DoWork += (ss, ee) =>
                {
                    mAdjustList = adjustmentBll.GetModelList("remark=" + CanID);
                };
            backWorker.RunWorkerCompleted += (ss, ee) =>
                {
                    if (mAdjustList != null)
                    {
                        foreach (Model.Adjustment model in mAdjustList)
                        {
                            string target = model.CanIDCmd.Remove(0, model.remark.Length);
                            switch (target)
                            {
                                case DC:
                                    txtDC.Text = model.Value.ToString();
                                    break;
                                case DE:
                                    txtDE.Text = model.Value.ToString();
                                    break;
                                case DI:
                                    txtDI.Text = model.Value.ToString();
                                    break;
                                case GF:
                                    txtGF.Text = model.Value.ToString();
                                    break;
                                case GE:
                                    txtGE.Text = model.Value.ToString();
                                    break;
                            }
                        }
                    }
                };
            backWorker.RunWorkerAsync();
        }
    }
}
